package com.showcai.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.showcai.dao.OrganizationMapper;
import com.showcai.module.*;
import com.showcai.myRunTime.MyRunTimeExcption;
import com.showcai.service.*;
import com.showcai.util.MD5Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * Created by baggio on 2016/12/27.
 */
@Service
public class OrganizationServiceImpl extends ServiceBase implements OrganizationService {

    @Autowired
    OrganizationMapper organizationMapper;



    @Override
    public void modifyOrganization(Organization organization) throws MyRunTimeExcption {
        if(organization.getTid()==null){
            organization.setCreateTime(new Date());
            organization.setPassWord(MD5Tools.MD5("123456"));
            organizationMapper.insertOrganization(organization);
            return;
        }
        organizationMapper.updateOrganizationById(organization);
    }

    @Override
    public void deleteOrganization(Organization organization) throws MyRunTimeExcption {
        organization.setStatus(BaseModule.STATUS_UNVALID);
        organizationMapper.updateOrganizationByPrimaryKeySelective(organization);
    }

    @Override
    public void operateOrganization(Long tid) throws MyRunTimeExcption {
        Organization organization = new Organization();
        organization.setStatus(BaseModule.STATUS_VALID);
        organization.setTid(tid);
        organizationMapper.updateOrganizationByPrimaryKeySelective(organization);
    }

    @Override
    public List<Organization> organizationList(Map<String, String> map) {
        List<Organization> list =organizationMapper.selectOrganizationList();
        return list;
    }

    @Override
    public PageInfo organizationPageList(Map<String, String> map) {
        Integer page = Integer.parseInt(map.get("page"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));
        PageHelper.startPage(page, pageSize);
        List<Organization> list =organizationMapper.selectOrganizationList();
        PageInfo pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public Organization selectOrganization(Long tid) {
        return organizationMapper.selectByPrimaryKey(tid);
    }

    @Override
    public Organization loginOrganization(Organization organization) {
        organization.setPassWord(MD5Tools.MD5(organization.getPassWord()));
        organization = organizationMapper.loginOrganization(organization);
        return organization;
    }

    @Override
    public void resetPassword(Long tid) {
        Organization organization = new Organization();
        organization.setTid(tid);
        organization.setPassWord(MD5Tools.MD5("123456"));
        organizationMapper.updateOrganizationByPrimaryKeySelective(organization);
    }
}
